//
//  JZHSHangPInViewController.m
//  极致荟
//
//  Created by hetao on 14-5-23.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHSHangPInViewController.h"
#import "JZHCommodity.h"

@interface JZHSHangPInViewController ()
{
   
}
@property (nonatomic,strong) JZHCommodity *comm;
@property (weak, nonatomic) IBOutlet UIScrollView *ag;
@property (weak, nonatomic) IBOutlet UIPageControl *page;
@property (weak, nonatomic) IBOutlet UIButton *but;

@end

@implementation JZHSHangPInViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initCommodity:(JZHCommodity *)commodity
{
    self=[[JZHSHangPInViewController alloc]init];
    if(self)
    {
        self.comm=commodity;
        return self;
    }
    return nil;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    if(OSVersionIsAtLeastiOS7>=7.0)
    {
        self.automaticallyAdjustsScrollViewInsets=NO;
    }
//    [self.but.layer setCornerRadius:6.0];
//    [self.but.layer setMasksToBounds:YES];
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
    [leftButton addTarget:self action:@selector(leftDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    self.navigationItem.title=@"商品详情";
    [self.myjg setKeyboardType:UIKeyboardTypeNumberPad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notkey:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    //启用scorll点击事件
    [self.scorll TapGestureEnend:YES];
    [self loadHttpData];
}

- (void)leftDrawerButtonPress:(UIButton *)but
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tapgest:(UITapGestureRecognizer *)tap
{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}
///添加商品图片
- (void)addimage:(NSArray *)arr 
{
    [_ag setContentSize:CGSizeMake(self.ag.frame.size.width*arr.count, 0)];
    if(arr.count>1)
    {
        [_page setNumberOfPages:arr.count];
    }else
    {
        [_page setNumberOfPages:0];
    }
    
    for(int i=0;i<arr.count;i++)
    {
        UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(i*self.ag.frame.size.width, 0, self.ag.frame.size.width, self.ag.frame.size.height)];
        //[ImageCache imageName:@"3.jpg" UiImageview:imageview];
        //[imageview setImage:[UIImage imageNamed:]];
        [ImageCache imageUrl:[arr objectAtIndex:i] UiImageview:imageview];
        [self.ag addSubview:imageview];
    }
    
}
///请求网络数据
- (void)loadHttpData
{
   // [MMProgressHUD showWithStatus:@"数据加载中..."];
    NSDictionary *dic= [[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInt:self.comm.comid],@"id", nil];
    [HttpRequest  httpRequestPost:JZHHTTPURL(@"auction/auctionView") parameters:dic block:^(id data, HttpReturnCode rerurncode) {
        if(rerurncode==0)
        {
            NSLog(@"%@",data);
            NSDictionary *datadic=[data objectForKey:@"data"];
            self.brand.text=[datadic objectForKey:@"brand"];
            self.coloor.text=[datadic objectForKey:@"color"];
            self.caizi.text=[datadic objectForKey:@"cz"];
            [self addimage:[[NSArray alloc]initWithObjects:[datadic objectForKey:@"images"], nil]];
            self.commname.text=[datadic objectForKey:@"name"];
            self.content.text=[datadic objectForKey:@"introduction"];
            [self.content sizeToFit];
            
            self.scj.text=[datadic objectForKey:@"marketPrice"];
            self.begen.text=[datadic objectForKey:@"startPrice"];
            self.dangjg.text=[datadic objectForKey:@"lastPrice"];
            self.comm.paprice=[[datadic objectForKey:@"lastPrice"] floatValue];
            [self.dangjg sizeToFit];
            self.comm.surplustime=[[datadic objectForKey:@"remainingTime"] longValue];
           NSTimer *timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(aouttime:) userInfo:nil repeats:YES];
            //self.comm
           
              [self aoutframe];
           
            [MMProgressHUD dismiss];
            
            
        }else
        {
           [MMProgressHUD showInformationWithTips:@"暂无数据"];
        }
    }];
}




///自动适配界面
- (void)aoutframe
{
    [self.view1 setFrame:CGRectMake(self.view1.frame.origin.x, self.view1.frame.origin.y, self.view1.frame.size.width, self.content.frame.size.height+26)];
    
   
    [self.view2 setFrame:CGRectMake(self.view2.frame.origin.x, self.view1.frame.origin.y+self.view1.frame.size.height, self.view2.frame.size.width, self.view2.frame.size.height)];
    
    [self.scorll setContentSize:CGSizeMake(0, self.view1.frame.size.height+self.view2.frame.size.height+self.view1.frame.origin.y)];
    
    [self.rmblable setFrame:CGRectMake(self.dangjg.frame.origin.x+self.dangjg.frame.size.width, self.rmblable.frame.origin.y, self.rmblable.frame.size.width, self.rmblable.frame.size.height)];
   
}

- (void)aouttime:(NSTimer *)time
{
    int  times=self.comm.surplustime-1;
    NSString *str=@"";
    int s=times/3600;
    int f=(times-s*
           3600)/60;
    int m=times-(s*3600+f*60);
    self.comm.surplustime=times;
    
    if(s<10)
    {
        str=[str stringByAppendingFormat:@"0%d:",s];
    }else
    {
        str=[str stringByAppendingFormat:@"%d:",s];
    }
    if(f<10)
    {
        str=[str stringByAppendingFormat:@"0%d:",f];
    }else
    {
        str=[str stringByAppendingFormat:@"%d:",f];
    }
    if(m<10)
    {
        str=[str stringByAppendingFormat:@"0%d",m];
    }else
    {
        str=[str stringByAppendingFormat:@"%d",m];
    }
    if(str.length>8)
    {
        [self.stime setFont:[UIFont systemFontOfSize:24]];
    }
    self.stime.text=str;
    
    
}

#pragma mark----scorllview delete-----
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
    
    CGFloat x=scrollView.contentOffset.x/scrollView.frame.size.width;
    if(fmod (x,1 )==0)
    {
        [_page setCurrentPage:x];
    }
}
- (IBAction)jp:(id)sender
{
    float f1=[self.myjg.text floatValue];
    if(f1<=self.comm.paprice)
    {
        UIAlertView *aler=[[UIAlertView alloc]initWithTitle:@"提示" message:@"出价不能比当前竞价低或者一样" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
        [aler show];
        return;
    }
    NSString *str = [NSString stringWithFormat:@"%.2f", f1];
    NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInt:[User defuceUser].userId],@"userId",[NSNumber numberWithInt:self.comm.comid],@"goodsId",str ,@"price", nil];
    [MMProgressHUD showWithStatus:@"提交中..."];
    [HttpRequest httpRequestPost:JZHHTTPURL(@"auction/auction") parameters:dic block:^(id data, HttpReturnCode rerurncode) {
        if(rerurncode==0)
        {
            NSLog(@"%@",data);
            [MMProgressHUD dismissWithSuccess:@"请到我的竞拍中查看" title:@"竞拍成功" afterDelay:2];
            [UINavigationController Pop:self];
        }else
        {
            if([[data objectForKey:@"status"] intValue]==-3)
            {
                [MMProgressHUD dismissWithError:@"竞拍价格无效" title:@"竞拍失败" afterDelay:1];
                return ;
            }
            if([[data objectForKey:@"status"] intValue]==-4)
            {
          
                [MMProgressHUD dismissWithError:@"拍卖以过期" title:@"竞拍失败" afterDelay:1];
                return ;
            }
            [MMProgressHUD dismissWithError:@"竞拍失败"];
        }
    }];
}


- (void)notkey:(NSNotification *)set
{
    CGPoint bpoint=[[[set userInfo] objectForKey:UIKeyboardCenterBeginUserInfoKey] CGPointValue];
    CGPoint epoint=[[[set userInfo] objectForKey:UIKeyboardCenterEndUserInfoKey] CGPointValue];
    float px= epoint.y- bpoint.y;
    [UIView animateWithDuration:0.3 animations:^{
        [self.view setCenter:CGPointMake(self.view.center.x, self.view.center.y+px)];
    }];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}
@end
